package com.dianping.cat.home.service.client.transform;

import com.dianping.cat.home.service.client.IEntity;
import com.dianping.cat.home.service.client.entity.ClientReport;
import com.dianping.cat.home.service.client.entity.Domain;
import com.dianping.cat.home.service.client.entity.Method;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;







public class DefaultJsonParser<T>
{
  private DefaultLinker m_linker;
  private Stack<String> m_tags;
  private Stack<Object> m_objs;
  private List<T> m_entities;
  private Class<?> m_entityClass;

  private DefaultJsonParser(Class<?> entityClass) {
/*  35 */     this.m_linker = new DefaultLinker(true);

/*  37 */     this.m_tags = new Stack();

/*  39 */     this.m_objs = new Stack();

/*  41 */     this.m_entities = new ArrayList();




/*  46 */     this.m_entityClass = entityClass;
  }


/*  50 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, InputStream in) throws IOException { return (T)parse(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> T parse(Class<T> entityClass, Reader reader) throws IOException {
/*  55 */     DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  57 */     parser.onArrayBegin();
/*  58 */     parser.parse(new JsonReader(reader));
/*  59 */     parser.onArrayEnd();

/*  61 */     if (parser.m_entities.isEmpty()) {
/*  62 */       return null;
    }
/*  64 */     return (T)(IEntity)parser.m_entities.get(0);
  }



/*  69 */   public static <T extends IEntity<T>> T parse(Class<T> entityClass, String json) throws IOException { return (T)parse(entityClass, new StringReader(json)); }



/*  73 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, InputStream in) throws Exception { return parseArray(entityClass, new InputStreamReader(in, "utf-8")); }



  public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, Reader reader) throws Exception {
    DefaultJsonParser parser = new DefaultJsonParser(entityClass);

/*  80 */     parser.parse(new JsonReader(reader));
/*  81 */     return (List<T>) parser.m_entities;
  }


/*  85 */   public static <T extends IEntity<T>> List<T> parseArray(Class<T> entityClass, String json) throws Exception { return parseArray(entityClass, new StringReader(json)); }




  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/*  91 */     if (value == null || value.length() == 0) {
/*  92 */       return defaultValue;
    }

    if (type == Boolean.class)
/*  96 */       return (T)Boolean.valueOf(value); 
/*  97 */     if (type == Integer.class)
/*  98 */       return (T)Integer.valueOf(value); 
/*  99 */     if (type == Long.class)
/* 100 */       return (T)Long.valueOf(value); 
/* 101 */     if (type == Short.class)
/* 102 */       return (T)Short.valueOf(value); 
/* 103 */     if (type == Float.class)
/* 104 */       return (T)Float.valueOf(value); 
/* 105 */     if (type == Double.class)
/* 106 */       return (T)Double.valueOf(value); 
/* 107 */     if (type == Byte.class)
/* 108 */       return (T)Byte.valueOf(value); 
/* 109 */     if (type == Character.class) {
/* 110 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 112 */     return (T)value;
  }


  private Object createRootEntity() {
    try {
/* 118 */       return this.m_entityClass.newInstance();

    }
/* 121 */     catch (Exception e) {
/* 122 */       throw new RuntimeException(String.format("Unable to create entity(%s) instance!", new Object[] { this.m_entityClass.getName() }), e);
    }
  }


/* 127 */   private boolean isTopLevel() { return (this.m_objs.size() == 1); }


  protected void onArrayBegin() {
/* 131 */     if (this.m_objs.isEmpty()) {
/* 132 */       this.m_objs.push(this.m_entities);
/* 133 */       this.m_tags.push("");
    }
  }
  protected void onArrayEnd() {
/* 137 */     this.m_objs.pop();
/* 138 */     this.m_tags.pop();
  }


/* 142 */   protected void onName(String name) { this.m_tags.push(name); }


  protected void onObjectBegin() {
/* 146 */     if (isTopLevel()) {
/* 147 */       this.m_objs.push(createRootEntity());
/* 148 */       this.m_tags.push("");
    } else {
/* 150 */       Object parent = this.m_objs.peek();
/* 151 */       String tag = (String)this.m_tags.peek();

/* 153 */       if (parent instanceof ClientReport) {
/* 154 */         if ("domains".equals(tag)) {
/* 155 */           this.m_objs.push(parent);
        } else {
/* 157 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 159 */           if ("domains".equals(parentTag)) {
/* 160 */             Domain domains = new Domain();

/* 162 */             this.m_linker.onDomain((ClientReport)parent, domains);
/* 163 */             this.m_objs.push(domains);
          } else {
/* 165 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
/* 168 */       } else if (parent instanceof Domain) {
/* 169 */         if ("methods".equals(tag)) {
/* 170 */           this.m_objs.push(parent);
        } else {
/* 172 */           String parentTag = (this.m_tags.size() >= 2) ? (String)this.m_tags.get(this.m_tags.size() - 2) : null;

/* 174 */           if ("methods".equals(parentTag)) {
/* 175 */             Method methods = new Method();

/* 177 */             this.m_linker.onMethod((Domain)parent, methods);
/* 178 */             this.m_objs.push(methods);
          } else {
/* 180 */             throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
          }
        }
      } else {
/* 184 */         throw new RuntimeException(String.format("Unknown tag(%s) found at %s!", new Object[] { tag, this.m_tags }));
      }
    }
  }

  protected void onObjectEnd() {
/* 190 */     this.m_tags.pop();

/* 192 */     Object entity = this.m_objs.pop();

/* 194 */     if (isTopLevel()) {
/* 195 */       this.m_entities.add((T)entity);
    }
  }

  protected void onValue(String value) {
/* 200 */     Object parent = this.m_objs.peek();
/* 201 */     String tag = (String)this.m_tags.pop();

/* 203 */     if (parent instanceof ClientReport) {
/* 204 */       parseForClientReport((ClientReport)parent, tag, value);
/* 205 */     } else if (parent instanceof Domain) {
/* 206 */       parseForDomain((Domain)parent, tag, value);
/* 207 */     } else if (parent instanceof Method) {
/* 208 */       parseForMethod((Method)parent, tag, value);
    } else {
/* 210 */       throw new RuntimeException(String.format("Unknown tag(%s) under %s!", new Object[] { tag, parent }));
    }
  }

  private void parse(JsonReader reader) throws IOException {
    try {
/* 216 */       reader.parse(this);
/* 217 */     } catch (EOFException e) {
/* 218 */       if (this.m_objs.size() > 1) {
/* 219 */         throw new EOFException(String.format("Unexpected end while parsing json! tags: %s.", new Object[] { this.m_tags }));
      }
    }

/* 223 */     this.m_linker.finish();
  }

  public void parseForClientReport(ClientReport clientReport, String tag, String value) {
/* 227 */     if (!"domains".equals(tag))
    {
/* 229 */       if ("domain".equals(tag)) {
/* 230 */         clientReport.setDomain(value);
/* 231 */       } else if ("start-time".equals(tag)) {
/* 232 */         clientReport.setStartTime(toDate(value, "yyyy-MM-dd HH:mm:ss"));
/* 233 */       } else if ("end-time".equals(tag)) {
/* 234 */         clientReport.setEndTime(toDate(value, "yyyy-MM-dd HH:mm:ss"));
      } else {
/* 236 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, clientReport, this.m_tags }));
      }  }
  }

  public void parseForDomain(Domain domain, String tag, String value) {
/* 241 */     if (!"methods".equals(tag))
    {
/* 243 */       if ("id".equals(tag)) {
/* 244 */         domain.setId(value);
/* 245 */       } else if ("totalCount".equals(tag)) {
/* 246 */         domain.setTotalCount(((Long)convert(Long.class, value, Long.valueOf(0L))).longValue());
/* 247 */       } else if ("failureCount".equals(tag)) {
/* 248 */         domain.setFailureCount(((Long)convert(Long.class, value, Long.valueOf(0L))).longValue());
/* 249 */       } else if ("failurePercent".equals(tag)) {
/* 250 */         domain.setFailurePercent(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 251 */       } else if ("sum".equals(tag)) {
/* 252 */         domain.setSum(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 253 */       } else if ("avg".equals(tag)) {
/* 254 */         domain.setAvg(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
      } else {
/* 256 */         throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, domain, this.m_tags }));
      }  }
  }

  public void parseForMethod(Method method, String tag, String value) {
/* 261 */     if ("id".equals(tag)) {
/* 262 */       method.setId(value);
/* 263 */     } else if ("service".equals(tag)) {
/* 264 */       method.setService(value);
/* 265 */     } else if ("totalCount".equals(tag)) {
/* 266 */       method.setTotalCount(((Long)convert(Long.class, value, Long.valueOf(0L))).longValue());
/* 267 */     } else if ("failureCount".equals(tag)) {
/* 268 */       method.setFailureCount(((Long)convert(Long.class, value, Long.valueOf(0L))).longValue());
/* 269 */     } else if ("failurePercent".equals(tag)) {
/* 270 */       method.setFailurePercent(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 271 */     } else if ("sum".equals(tag)) {
/* 272 */       method.setSum(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 273 */     } else if ("avg".equals(tag)) {
/* 274 */       method.setAvg(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 275 */     } else if ("qps".equals(tag)) {
/* 276 */       method.setQps(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
/* 277 */     } else if ("timeout".equals(tag)) {
/* 278 */       method.setTimeout(((Double)convert(Double.class, value, Double.valueOf(0.0D))).doubleValue());
    } else {
/* 280 */       throw new RuntimeException(String.format("Unknown tag(%s) of %s under %s!", new Object[] { tag, method, this.m_tags }));
    }
  }


  protected Date toDate(String str, String format) {
    try {
/* 287 */       return (new SimpleDateFormat(format)).parse(str);
/* 288 */     } catch (ParseException e) {
/* 289 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
  static class JsonReader { private Reader m_reader;
    private char[] m_buffer;

    public JsonReader(Reader reader) {
/* 296 */       this.m_buffer = new char[2048];






/* 303 */       this.m_reader = reader;
    }
    private int m_size; private int m_index;
    private char next() throws IOException {
/* 307 */       if (this.m_index >= this.m_size) {
/* 308 */         this.m_size = this.m_reader.read(this.m_buffer);
/* 309 */         this.m_index = 0;

/* 311 */         if (this.m_size == -1) {
/* 312 */           throw new EOFException();
        }
      }

/* 316 */       return this.m_buffer[this.m_index++];
    }

    public void parse(DefaultJsonParser parser) throws IOException {
/* 320 */       StringBuilder sb = new StringBuilder();
/* 321 */       boolean flag = false;

      while (true) {
/* 324 */         char ch = next();

/* 326 */         switch (ch) {
          case '\t':
          case '\n':
          case '\r':
          case ' ':
            continue;
          case '{':
/* 333 */             parser.onObjectBegin();
/* 334 */             flag = false;
            continue;
          case '}':
/* 337 */             if (flag) {
/* 338 */               parser.onValue(sb.toString());
/* 339 */               sb.setLength(0);
            }

/* 342 */             parser.onObjectEnd();
/* 343 */             flag = false;
            continue;
          case '"':
          case '\'':
            while (true) {
/* 348 */               char ch2 = next();

/* 350 */               if (ch2 != ch) {
/* 351 */                 if (ch2 == '\\') {
/* 352 */                   char ch3 = next();

/* 354 */                   switch (ch3) {
                    case 't':
/* 356 */                       sb.append('\t');
                      continue;
                    case 'r':
/* 359 */                       sb.append('\r');
                      continue;
                    case 'n':
/* 362 */                       sb.append('\n');
                      continue;
                  }
/* 365 */                   sb.append(ch3);

                  continue;
                }
/* 369 */                 sb.append(ch2); continue;
              }  break;
            }
/* 372 */             if (!flag) {
/* 373 */               parser.onName(sb.toString());
            } else {
/* 375 */               parser.onValue(sb.toString());
/* 376 */               flag = false;
            }

/* 379 */             sb.setLength(0);
            continue;




          case ':':
/* 386 */             if (sb.length() != 0) {
/* 387 */               parser.onName(sb.toString());
/* 388 */               sb.setLength(0);
            }

/* 391 */             flag = true;
            continue;
          case ',':
/* 394 */             if (sb.length() != 0) {
/* 395 */               if (!flag) {
/* 396 */                 parser.onName(sb.toString());
              } else {
/* 398 */                 parser.onValue(sb.toString());
              }

/* 401 */               sb.setLength(0);
            }

/* 404 */             flag = false;
            continue;
          case '[':
/* 407 */             parser.onArrayBegin();
/* 408 */             flag = false;
            continue;
          case ']':
/* 411 */             parser.onArrayEnd();
/* 412 */             flag = false;
            continue;
        }
/* 415 */         sb.append(ch);
      }
    } }

}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/client/transform/DefaultJsonParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */